package cn.edu.njuit.children.apis;

import cn.edu.njuit.children.common.Result;
import cn.edu.njuit.children.entity.Children;
import cn.edu.njuit.children.entity.UserAccount;
import cn.edu.njuit.children.entity.model.ChildrenModel;
import cn.edu.njuit.children.mapper.ChildrenMapper;
import cn.edu.njuit.children.service.ChildrenService;
import cn.edu.njuit.children.service.UserService;
import cn.edu.njuit.children.service.impl.BaiduAiService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.*;
import kotlin.ParameterName;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Description: TODO
 * @Data: 2022/11/29 14:54
 * @Author: guzy
 */
@RestController
@RequestMapping("/api/children")
@Api(value = "幼儿接口", tags = "幼儿接口")
public class ChildrenApi {

    @Autowired
    ChildrenService service;
    @Autowired
    ChildrenMapper mapper;
    @Autowired
    UserService userService;
    @Autowired
    BaiduAiService baiduAiService;

    @PostMapping
    public Result save(@Valid ChildrenModel o) {
        boolean b = service.addChildren(o);
        if (b){
            return Result.success("添加成功");
        }else {
            return Result.failure("添加失败");
        }
    }



    @PutMapping
    public Result update(@Valid ChildrenModel o) throws Exception {
        boolean b = service.upChildren(o);
        if (b){
            return Result.success("修改成功");
        }else {
            return Result.failure("修改失败");
        }
    }

    @DeleteMapping("{id}")
    public Result delete(@PathVariable Long id) throws Exception {
        boolean b = service.deleteChildren(id);
        if (b){
            return Result.success("删除成功");
        }else {
            return Result.failure("删除失败");
        }
    }

    @GetMapping("{id}")
    public Result getById(@PathVariable Long id) {
        Children byId = service.getById(id);
        if (byId!=null){
            return Result.success(service.getById(id));
        }else {
            return Result.failure("无数据");
        }
    }

    @GetMapping
    public Result getPage(
            @RequestParam(defaultValue = "0",required = false) Integer currentPage,
            @RequestParam(defaultValue = "10",required = false) Integer pageSize,
            @RequestParam(required = false) String keyWords) {
        String phone = ((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername();
        UserAccount account = userService.getAccount(phone);
        LambdaQueryWrapper<Children> wrapper = new LambdaQueryWrapper<Children>();
        if (keyWords!=null){
            wrapper.eq(Children::getUserId,account.getId())
                    .like(Children::getCdNickname,keyWords);
        }else {
            wrapper.eq(Children::getUserId,account.getId());
        }
        IPage<Children> page = new Page<Children>(currentPage, pageSize);
        mapper.selectPage(page, wrapper);
        return Result.success(page);
    }

}
